package model

import (
	"fmt"
	"myserver/app/types"
	"myserver/pkg/global"
	"net/http"
	"net/url"

	"github.com/tencentyun/cos-go-sdk-v5"
)

func CeatCosClient() {

	u, e := url.Parse(global.CONFIG.Cos.Url)
	if e != nil {
		fmt.Println(e.Error())
		return
	}
	b := &cos.BaseURL{BucketURL: u}
	a := cos.NewClient(b, &http.Client{
		Transport: &cos.AuthorizationTransport{
			SecretID:  global.CONFIG.Cos.SecretId,
			SecretKey: global.CONFIG.Cos.SecretKey,
		},
	})
	global.COS = a
}

func CreateSysFileTable() {
	p := &types.SysFile{}
	CeatCosClient()
	tables = append(tables, *p)
}

// 添加一个文件
func AddFile(m *types.SysFile) error {
	var err error
	if err = db.Create(m).Error; err != nil {
		return err
	}
	return err
}

// 根据uuid 查询
func FindFileByUuid(strs []string) ([]types.SysFile, error) {
	var files []types.SysFile
	var err error
	tx := db.Begin()
	err = tx.Where("uuid IN ? AND is_del=?", strs, 0).Find(&files).Error
	if err != nil {
		tx.Rollback()
	} else {
		tx.Commit()
	}

	return files, err
}

// 更新文件信息
func UpDataFiles(list *[]types.SysFile) error {
	var err error
	tx := db.Begin()
	tx.Save(list)
	// if err != nil {
	// 	tx.Rollback()
	// } else {
	tx.Commit()
	// }
	return err

}
